{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a61f22bc-43e5-483c-ae79-4b4a6c171052",
   "metadata": {},
   "source": [
    "# 单层网络模型\n",
    "\n",
    "本小节主要介绍单层神经网络模型的设计，使用LeNet5的变体作为讲解实例。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e358297b-4083-4ebe-9ad4-5f967a824deb",
   "metadata": {},
   "source": [
    "首先需要导入所需的minspore包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2c60774c-e491-40d8-958d-62093643fc93",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mindspore.nn as nn\n",
    "from mindspore.common.initializer import Normal"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49dfb108-c545-4e91-8bde-768f91f1e4ab",
   "metadata": {},
   "source": [
    "其次定义单层网络，在定义中，我们将所需的运算设置为卷积，不需要层。在构建前向网络时，构建单层卷积和relu激活函数得到定义好的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "31496045-52d2-4197-bc06-6015a40b1880",
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNet5(nn.Cell):\n",
    "    \"\"\"\n",
    "    Lenet网络结构\n",
    "    \"\"\"\n",
    "    def __init__(self, num_class=10, num_channel=1):\n",
    "        super(LeNet5, self).__init__()\n",
    "        # 定义所需要的运算\n",
    "        self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')\n",
    "\n",
    "    def construct(self, x):\n",
    "        # 使用定义好的运算构建前向网络\n",
    "        x = self.conv1(x)\n",
    "        x = self.relu(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ef2e459-a733-4370-b571-f7258fde197e",
   "metadata": {},
   "source": [
    "对模型进行调用，得到结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9256d88b-5ab8-49d8-bab3-1a7a96f45993",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('conv1.weight', Parameter (name=conv1.weight, shape=(6, 1, 5, 5), dtype=Float32, requires_grad=True))\n"
     ]
    }
   ],
   "source": [
    "model = LeNet5()\n",
    "for m in model.parameters_and_names():\n",
    "    print(m)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
